package cn.tedu.csmall.product.ex.hander;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.web.JsonResult;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Set;
import java.util.StringJoiner;

@Slf4j
@RestControllerAdvice
public class GlobalException {
    @ExceptionHandler
    public JsonResult handleServiceException(ServiceException e){
        log.warn("程序运行过程中出现ServiceException，将统一处理！");
        log.warn("异常信息：{}",e.getMessage());
        return JsonResult.fail(e);
    }

    @ExceptionHandler
    public JsonResult handleServiceException(BindException e){
        log.warn("程序运行过程中出现BindException，将统一处理！");
        log.warn("异常信息：{}",e.getMessage());
        /*StringJoiner sj = new StringJoiner(",","请求参数格式错误！"
                                           ,"!");
        List<FieldError> list =  e.getFieldErrors();
        for (FieldError fieldError : list){
            sj.add(fieldError.getDefaultMessage());
        }*/
        return JsonResult.fail(ServiceCode.ERR_BAD_REQUEST,e.getFieldError().getDefaultMessage());
    }

    //处理
    @ExceptionHandler({InternalAuthenticationServiceException.class,
            BadCredentialsException.class})
    public JsonResult<Void> handleAuthenticationException(AuthenticationException e){
        log.warn("程序运行过程中出现AuthenticationException，将统一处理！");
        log.warn("异常信息：{}", e.getMessage());
        String message = "登录失败，用户名或密码错误!";
        return JsonResult.fail(ServiceCode.ERR_UNAUTHORIZED,message);
    }

    @ExceptionHandler
    public JsonResult<Void> handleDisabledException(DisabledException e){
        log.warn("程序运行过程中出现DisabledException，将统一处理！");
        log.warn("异常信息：{}", e.getMessage());
        String message = "登录失败，此账号已经被禁用!";
        return JsonResult.fail(ServiceCode.ERR_UNAUTHORIZED_DISABLE,message);
    }

    @ExceptionHandler
    public JsonResult<Void> handleAccessDeniedException(AccessDeniedException e){
        log.warn("程序运行过程中出现AccessDeniedException，将统一处理！");
        log.warn("异常信息：{}", e.getMessage());
        String message = "禁止访问!您当前登录的账号无此操作权限！";
        return JsonResult.fail(ServiceCode.ERR_FORBIDDEN,message);
    }

    @ExceptionHandler
    public JsonResult handleThrowable(Throwable e){
        log.warn("程序运行过程中出现Throwable，将统一处理！");
        log.warn("异常类型：{}",e.getClass());
        log.warn("异常信息：{}",e.getMessage());
        String message = "[开发阶段专用]你的服务器出现了异常，请检查服务器控制台中的" +
                "错误信息,并补充处理此类异常的方法！";
        //String message = "服务器忙，请稍后尝试"; //项目上线时应该使用此提示文本
        e.printStackTrace(); //打印异常的跟踪信息，主要是为了在开发阶段更好的检查出异常的原因
        return JsonResult.fail(ServiceCode.ERR_UNKNOWN,message);
    }

    @ExceptionHandler
    public JsonResult handleConstraintViolationException(ConstraintViolationException e){
        log.warn("程序运行时出现ConstraintViolationException，将统一处理");
        log.warn("异常信息：{}",e.getMessage());
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        String message = "";
        for (ConstraintViolation<?> constraintViolation: constraintViolations){
            message = constraintViolation.getMessage();
        }
        return JsonResult.fail(ServiceCode.ERR_BAD_REQUEST,message);
    }
}
